package com.hhc.repository;

import com.hhc.entity.NoticeAnnouncement;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
 * <p>
 *   通知公告数据访问接口
 * </p>
 * @author pc
 */
@Repository
public interface NoticeRepository extends JpaRepository<NoticeAnnouncement, Long> {

    /**
     * 分页查询有效的通知公告信息
     *
     * @param now 当前时间
     * @param isActive 是否激活状态
     * @return 有效公告列表
     */
    @Query("SELECT n FROM NoticeAnnouncement n WHERE (n.expireTime IS NULL OR n.expireTime > :now) AND n.isActive = :isActive ORDER BY n.createTime DESC")
    List<NoticeAnnouncement> findActiveNotices(@Param("now") LocalDateTime now, @Param("isActive") int isActive);

    // 新增方法：带条件的查询
    @Query("SELECT n FROM NoticeAnnouncement n WHERE " +


            "(n.expireTime IS NULL OR n.expireTime > :now) AND " +
            "n.isActive = :isActive AND "+
            "(:#{#title} IS NULL OR n.title LIKE :#{#title}) AND " +
            "(:#{#content} IS NULL OR n.content LIKE :#{#content})")
    Page<NoticeAnnouncement> findActiveNoticesWithCondition(
            @Param("now") LocalDateTime now,
            @Param("isActive") Integer isActive,
            @Param("title") String title,
            @Param("content") String content,
            Pageable pageable);
}
